package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"week/cache"
	"week/models"
)

type NewsController struct {
	beego.Controller
}

// msg
type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func NewList() {
	news := models.News{}
	key := "news"
	models.DB.Find(&news)
	jsonData, _ := json.Marshal(&news)
	cache.Rdb.Set(cache.Ctx, key, jsonData, 0)
}

// 新闻添加
func (c *NewsController) Add() {
	var err error
	//接收数据
	title := c.GetString("title")
	news_types := c.GetString("news_types")

	//为空验证
	if title == "" || news_types == "" {
		res := Response{
			Code: 500,
			Msg:  "参数不能为空",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	//接收news数据库
	var news models.News
	news.Title = title
	news.NewsTypes = news_types

	//调用方法，实现添加
	err = news.Add()
	if err != nil {
		res := Response{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	//缓存数据redis
	cacheData, _ := news.List()
	jsonData, _ := json.Marshal(cacheData)
	cache.Rdb.Set(cache.Ctx, "news_add", jsonData, 0)

	res := Response{
		Code: 200,
		Msg:  "添加成功",
		Data: nil,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return

}

// 新闻列表
func (c *NewsController) List() {
	//获取数据
	cacheData := cache.Rdb.Get(cache.Ctx, "news_list").Val()
	//接收结构体
	var news []*models.News

	//调用方法,实现列表
	if cacheData == "" {
		models.DB.Find(&news)
		jsonData, _ := json.Marshal(&news)
		cache.Rdb.Set(cache.Ctx, "news_list", jsonData, 0)
	} else {
		json.Unmarshal([]byte(cacheData), &news)
	}

	res := Response{
		Code: 200,
		Msg:  "获取成功",
		Data: news,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return

}

// 新闻修改
func (c *NewsController) Update() {
	//接收数据
	id, _ := c.GetInt("id")
	title := c.GetString("title")
	news_types := c.GetString("news_types")
	//验证id
	if id < 1 {
		res := Response{
			Code: 500,
			Msg:  "参数不正确",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	//接收结构体
	var news models.News
	news.ID = uint(id)
	news.Title = title
	news.NewsTypes = news_types
	//调用方法，实现修改
	if news.Update() != nil {
		res := Response{
			Code: 500,
			Msg:  "修改失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	//缓存数据
	cacheData, _ := news.List()
	jsonData, _ := json.Marshal(cacheData)
	cache.Rdb.Set(cache.Ctx, "news_update", jsonData, 0)

	res := Response{
		Code: 200,
		Msg:  "修改成功",
		Data: news,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return

}

// 新闻删除
func (c *NewsController) Del() {
	//接收id
	id, _ := c.GetInt("id")
	//验证id
	if id < 1 {
		res := Response{
			Code: 500,
			Msg:  "参数不正确",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	//接收结构体
	var news models.News
	news.ID = uint(id)

	if news.Del(id) != nil {
		res := Response{
			Code: 500,
			Msg:  "删除失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	//缓存数据
	cacheData, _ := news.List()
	jsonData, _ := json.Marshal(cacheData)
	cache.Rdb.Set(cache.Ctx, "news_del", jsonData, 0)

	res := Response{
		Code: 200,
		Msg:  "删除成功",
		Data: news,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return
}
